package com.jd.ty.util;

import java.lang.reflect.Field;

import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.util.HqlCriterions;

public class Filter {

	public class Item {
		private String name;
		private Object value;
		private String exp = QueryConstants.EQ;

		public void setValue(Object value, String exp) {
			this.value = value;
			this.exp = exp;
		}

		public Item(String name) {
			this.name = name;
		}

		public Item(String name, Object value) {
			this.name = name;
			this.value = value;
		}

		public Item(String name, Object value, String exp) {
			this.name = name;
			this.value = value;
			this.exp = exp;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Object getValue() {
			return value;
		}

		public void setValue(Object value) {
			this.value = value;
		}

		public String getExp() {
			return exp;
		}

		public void setExp(String exp) {
			this.exp = exp;
		}

	}

	public String toHqlString() {
		HqlCriterions hqlStr = new HqlCriterions();
		Field[] fields = getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			try {
				field.setAccessible(true);
				if (field.get(this) instanceof Item) {
					Item item = (Item) field.get(this);
					Object obj = item.getValue();
					if (obj != null && !obj.equals("")) {
						hqlStr.and(item.getName(), item.getValue(), item.getExp());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return hqlStr.getHqlString();

	}
}
